﻿using Space_X.Models;
using Space_X.Properties;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace Space_X.ViewModels
{
    public class PanCamCollection : ObservableCollection<PanCamImage>
    {
        private CameraViewModel vModel;
        private FileSystemWatcher watcher = new FileSystemWatcher((string)Settings.Default.Path + "img", "*.bmp");
        private delegate void AddImageDelegate(params object[] args);
        private string currentPath;
        private DateTime initialWrite = DateTime.Now;

        public PanCamCollection(CameraViewModel viewModel) : base()
        {
            vModel = viewModel;

            string[] paths = Directory.GetFiles((string)Settings.Default.Path + "img", "*.bmp");

            foreach(string path in paths){

                BitmapImage bitmap = new BitmapImage(new Uri(path));

                PanCamImage imageToAdd = new PanCamImage { PanCamBitmap = bitmap, Name = path.Substring(path.LastIndexOf('\\') + 1), Resolution = bitmap.PixelWidth + "px x " + bitmap.PixelHeight + "px", Temperature = 35, Exposure = 100};
                Add(imageToAdd);
            }
        }

        /// <summary>
        /// Initializes the filewatcher thread
        /// </summary>
        public void InitFileWatcher()
        {
            watcher.EnableRaisingEvents = true;
            watcher.Created += new System.IO.FileSystemEventHandler(OnPhotoCreated);
        }

        /// <summary>
        /// File watcher listener that is called when a new image file is created
        /// </summary>
        /// <param name="sender">The  Collecton</param>
        /// <param name="e">The filepath parameters</param>
        private void OnPhotoCreated(object sender, FileSystemEventArgs e)
        {
            TimeSpan timeSinceWrite = DateTime.Now - initialWrite;

            // Since a bitmap is written in two steps, we must prevent the second write to trigger
            if (timeSinceWrite.TotalMilliseconds > 1000)
            {
                AddImageDelegate addImagedelegate = new AddImageDelegate(AddImageToCollection);
                currentPath = e.FullPath;
                Application.Current.Dispatcher.BeginInvoke(addImagedelegate, new object[1]);

                initialWrite = DateTime.Now;
            }
        }

        /// <summary>
        /// Delegate that is invoked when the file watcher thread sees that an image has been added to the observed fodler
        /// </summary>
        /// <param name="args">The delegate paramaeters</param>
        private void AddImageToCollection(params object[] args)
        {
            BitmapImage bitmap = new BitmapImage(new Uri(currentPath));

            PanCamImage imageToAdd = new PanCamImage { PanCamBitmap = bitmap, Name = currentPath.Substring(currentPath.LastIndexOf('\\') + 1), Resolution = bitmap.PixelWidth + "px x " + bitmap.PixelHeight + "px", Temperature = 35, Exposure = 100 };
            Add(imageToAdd);
        }
    }
}
